# -*- encoding: utf-8 -*-
############################################################################
#    Copyright (C) 2007 by Håvard Gulldahl                                 #
#    havard@gulldahl.no                                                    #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import sys, logging, datetime, cPickle as pickle
from string import join
import storm.exceptions as storm_exceptions, storm.locals as storm
import jeje

__author__ = u'Håvard Gulldahl <havard@gulldahl.no>'


class ejeje_org_gruppe(jeje.ejeje_storm):
  __storm_table__ = 'org_gruppe'
  _sql = """CREATE TABLE org_gruppe (
  id MEDIUMINT UNSIGNED auto_increment,
  navn VARCHAR(60),
  bruker_id MEDIUMINT UNSIGNED,
  opprettet TIMESTAMP,
  kommentar VARCHAR(256),
  PRIMARY KEY(id));"""
  id = storm.Int(primary=True)
  navn = storm.Unicode()
  bruker_id = storm.Int()
  bruker = storm.Reference(bruker_id, 'ejeje_bruker.id')
  opprettet = storm.DateTime(default=datetime.datetime.now())
  kommentar = storm.Unicode()

class ejeje_bruker(jeje.ejeje_storm):
  __storm_table__ = 'bruker'
  _sql = """CREATE TABLE bruker (
  id MEDIUMINT UNSIGNED auto_increment,
  navn VARCHAR(60),
  epost VARCHAR(128),
  lokal_epost VARCHAR(128),
  passord VARCHAR(10),
  sertifikat TINYBLOB,
  innlogget BOOLEAN DEFAULT 0,
  stanset BOOLEAN DEFAULT 0,
  slettet BOOLEAN DEFAULT 0,
  opprettet TIMESTAMP,
  kommentar VARCHAR(256),
  PRIMARY KEY(id));
  """
  id = storm.Int(primary=True)
  navn = storm.Unicode()
  epost = storm.Unicode()
  lokal_epost = storm.Unicode()
  passord = storm.Unicode()
  sertifikat = storm.Pickle()
  opprettet = storm.DateTime(default=datetime.datetime.now())
  innlogget = storm.Bool(default=False)
  stanset = storm.Bool(default=False)
  slettet = storm.Bool(default=False)
  kommentar = storm.Unicode()
  overvaking = storm.ReferenceSet(id, 'ejeje_bruker_overvaking.bruker_id') 
  favoritter = storm.ReferenceSet(id, 'ejeje_bruker_favoritter.bruker_id')
  sok = storm.ReferenceSet(id, 'ejeje_bruker_sok.bruker_id')
  innsyn = storm.ReferenceSet(id, 'ejeje_bruker_innsyn.bruker_id')
  
class ejeje_bruker_overvaking(jeje.ejeje_storm):
  __storm_table__ = 'bruker_overvaking'
  _sql = """CREATE TABLE bruker_overvaking (
  id MEDIUMINT UNSIGNED auto_increment,
  ord VARCHAR(128),
  bruker_id MEDIUMINT UNSIGNED,
  opprettet TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(id));
  """
  id = storm.Int(primary=True)
  ord = storm.Unicode()
  bruker_id = storm.Int()
  bruker = storm.Reference(bruker_id, ejeje_bruker.id)
  opprettet = storm.DateTime(default=datetime.datetime.now())
  
  def __init__(self, o):
    self.ord = o

class ejeje_bruker_favoritter(jeje.ejeje_storm):
  __storm_table__ = 'bruker_favoritter'
  _sql = """CREATE TABLE bruker_favoritter (
  id MEDIUMINT UNSIGNED auto_increment,
  bruker_id MEDIUMINT UNSIGNED,
  journalpost_id MEDIUMINT UNSIGNED,
  opprettet TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(id));
  """
  
  id = storm.Int(primary=True)
  bruker_id = storm.Int()
  bruker = storm.Reference(bruker_id, ejeje_bruker.id)
  journalpost_id = storm.Int()
  journalpost = storm.Reference(journalpost_id, jeje.ejeje_journalpost.id)
  opprettet = storm.DateTime(default=datetime.datetime.now())

  def __init__(self, i):
    self.journalpost_id = i

class ejeje_bruker_sok(jeje.ejeje_storm):
  __storm_table__ = 'bruker_sok'
  _sql = """CREATE TABLE bruker_sok (
  id MEDIUMINT UNSIGNED auto_increment,
  uttrykk VARCHAR(256),
  bruker_id MEDIUMINT UNSIGNED,
  opprettet TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(id));
  """
  id = storm.Int(primary=True)
  uttrykk = storm.Unicode()
  bruker_id = storm.Int()
  bruker = storm.Reference(bruker_id, ejeje_bruker.id)
  opprettet = storm.DateTime(default=datetime.datetime.now())
  
  def __init__(self, q):
    self.uttrykk = q

class ejeje_bruker_innsyn(jeje.ejeje_storm):
  __storm_table__ = 'bruker_innsyn'
  _sql = """CREATE TABLE bruker_innsyn (
  id MEDIUMINT UNSIGNED auto_increment,
  bruker_id MEDIUMINT UNSIGNED,
  opprettet TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  journalpost_id MEDIUMINT UNSIGNED,
  foresporsel_sendt TIMESTAMP,
  svar_mottatt TIMESTAMP,
  lest BOOLEAN NOT NULL DEFAULT 0,
  kommentar VARCHAR(255),
  PRIMARY KEY(id));
  """
  id = storm.Int(primary=True)
  bruker_id = storm.Int()
  bruker = storm.Reference(bruker_id, ejeje_bruker.id)
  journalpost_id = storm.Int()
  journalpost = storm.Reference(journalpost_id, jeje.ejeje_journalpost.id)
  opprettet = storm.DateTime(default=datetime.datetime.now())
  foresporsel_sendt = storm.DateTime()
  svar_mottatt = storm.DateTime()
  kommentar = storm.Unicode()
  lest = storm.Bool(default=False)

  def __init__(self, jpid):
    self.journalpost_id = jpid
